<style lang="less">
@import '../../../styles/table-common.less';
</style>
<template>
  <div class="search">
    <Card>
      <Row @keydown.enter.native="handleSearch">
        <Form ref="searchForm" :model="searchForm" inline :label-width="70">
          <FormItem label="帖子内容" prop="content" style="width: 300px;">
            <Input type="text" v-model="searchForm.content" clearable placeholder="请输入帖子内容" style="width: 200px" />
          </FormItem>
          <FormItem label="审核状态" prop="checkStatus" style="width: 300px;">
            <Select v-model="searchForm.checkStatus">
              <Option :value="0">正常帖子</Option>
              <Option :value="1">举报帖子</Option>
            </Select>
          </FormItem>

          <FormItem style="margin-left: -35px" class="br">
            <Button @click="handleSearch" type="primary" icon="ios-search">搜索</Button>
            <Button @click="handleReset">重置</Button>
          </FormItem>
        </Form>
      </Row>

      <Row>
        <Table
            :loading="loading"
            border
            :columns="columns"
            :data="data"
            sortable="custom"
            @on-sort-change="changeSort"
            @on-selection-change="showSelect"
            ref="table"
        ></Table>
      </Row>
      <Row type="flex" justify="end" class="page">
        <Page
            :current="searchForm.pageNum"
            :total="total"
            :page-size="searchForm.pageSize"
            @on-change="changePage"
            @on-page-size-change="changePageSize"
            :page-size-opts="[10, 20, 50]"
            size="small"
            show-total
            show-elevator
            show-sizer
        ></Page>
      </Row>
    </Card>

    <postComment :data="form" v-model="showUser" @on-submit="getDataList" />

    <postDetail :data="form" v-model="detailShow" @on-submit="getDataList" />
  </div>
</template>

<script>
import { getPostList, removePostById, topPostById, setSwiperPostById } from '@/api/new.js';
import postComment from './postComment.vue';
import postDetail from './postDetail.vue';
export default {
  name: 'post-manage',
  components: {
    postComment,
    postDetail
  },
  data() {
    return {
      height: 510,
      showUser: false,
      detailShow: false,
      showType: '0',
      loading: true,
      loadingExport: true,
      modalExportAll: false,
      selectList: [],
      searchKey: '',
      searchForm: {
        content: '',
        pageNumber: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        postType: 1,
        checkStatus: ''
      },
      form: {},
      columns: [
        {
          type: 'index',
          width: 60,
          align: 'center',
          fixed: 'left'
        },
        {
          title: '帖子内容',
          key: 'content',
          minWidth: 180,
          render: (h, params) => {
            return h('div', [
              h('span', {
                style: {
                  display: 'inline-block',
                  width: '100%',
                  overflow: 'hidden',
                  textOverflow: 'ellipsis',
                  whiteSpace: 'nowrap',
                  lineClamp: 2
                },
                domProps: {
                  title: params.row.content
                }
              }, params.row.content)
            ]);

          }
        },
        {
          title: '浏览次数',
          key: 'historyCount',
          align: 'center',
          minWidth: 110
        },
        {
          title: '审核状态',
          key: 'checkStatus',
          align: 'center',
          width: 110,
          render: (h, params) => {
            if (params.row.checkStatus === 0) {
              return h('span', '审核中')
            } else if (params.row.checkStatus === 1) {
              return h('span', '未审核通过')
            } else {
              return h('span', '--')
            }
          }
        },
        {
          title: '置顶状态',
          key: 'topStatus',
          align: 'center',
          width: 110,
          render: (h, params) => {
            if (params.row.topStatus === 0) {
              return h('span', '未置顶')
            } else if (params.row.topStatus === 1) {
              return h('span', '已置顶')
            } else {
              return h('span', '--')
            }
          }
        },
        {
          title: '发帖时间',
          key: 'createTime',
          align: 'center',
          minWidth: 125
        },
        {
          title: '操作',
          key: 'action',
          width: 330,
          align: 'center',
          fixed: 'right',
          render: (h, params) => {
            return h('div', [
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.showDetail(params.row);
                      }
                    }
                  },
                  '详情'
              ),
              h('Divider', {
                props: {
                  type: 'vertical'
                }
              }),
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.remove(params.row);
                      }
                    }
                  },
                  '删除'
              ),
              h('Divider', {
                props: {
                  type: 'vertical'
                }
              }),
              ,
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.showComment(params.row);
                      }
                    }
                  },
                  '查看评论'
              ),
              h('Divider', {
                props: {
                  type: 'vertical'
                }
              }),
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.setPostTop(params.row);
                      }
                    }
                  },
                  '置顶'
              ),
              h('Divider', {
                props: {
                  type: 'vertical'
                }
              }),
              h(
                  'a',
                  {
                    on: {
                      click: () => {
                        this.setPostSwiper(params.row);
                      }
                    }
                  },
                  '设为轮播'
              )
            ]);
          }
        }
      ],
      data: [],
      total: 0
    };
  },
  methods: {
    init() {
      this.getDataList();
    },
    changePage(v) {
      this.searchForm.pageNumber = v;
      this.getDataList();
      this.clearSelectAll();
    },
    changePageSize(v) {
      this.searchForm.pageSize = v;
      this.getDataList();
    },
    getDataList() {
      // 多条件搜索用户列表
      this.loading = true;
      getPostList(this.searchForm).then(res => {
        this.loading = false;
        if (res.code === 200) {
          this.data = res.data.records;
          this.total = res.data.total;
          if (this.data.length == 0 && this.searchForm.pageNumber > 1) {
            this.searchForm.pageNumber -= 1;
            this.getDataList();
          }
        }
      });
    },
    handleSearch() {
      this.searchForm.pageNum = 1;
      this.searchForm.pageSize = 10;
      this.getDataList();
    },
    handleReset() {
      this.$refs.searchForm.resetFields();
      this.searchForm.pageNum = 1;
      this.searchForm.pageSize = 10;
      // 重新加载数据
      this.getDataList();
    },
    changeSort(e) {
      this.searchForm.sort = e.key;
      this.searchForm.order = e.order;
      if (e.order == 'normal') {
        this.searchForm.order = '';
      }
      this.getDataList();
    },

    // 查看评论
    showComment(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = '';
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      this.form = data;
      this.showUser = true;
    },

    // 查看详情
    showDetail(v) {
      // 转换null为""
      for (let attr in v) {
        if (v[attr] == null) {
          v[attr] = '';
        }
      }
      let str = JSON.stringify(v);
      let data = JSON.parse(str);
      console.log(data, '22222')
      this.form = data;
      console.log(this.form, '3333')
      this.title = '详情';
      this.detailShow = true;
      console.log('11111')
    },

    // 设为置顶
    setPostTop(v) {
      this.$Modal.confirm({
        title: '置顶',
        content: '确定要置顶该条帖子吗？',
        loading: true,
        onOk: () => {
          topPostById({ postId: v.id }).then(res => {
            this.$Modal.remove();
            if (res.code === 200) {
              this.$Message.success('置顶成功');
              this.getDataList();
            }
          })
        }
      })
    },

    // 设为轮播图
    setPostSwiper(v) {
      this.$Modal.confirm({
        title: '设置',
        content: '确定要设置该条帖子为轮播图吗？',
        loading: true,
        onOk: () => {
          setSwiperPostById(v.id).then(res => {
            this.$Modal.remove();
            if (res.code === 200) {
              this.$Message.success('设置成功');
              this.getDataList();
            } else {
              this.$Message.error(res.data.message);
            }
          })
        }
      })
    },

    remove(v) {
      this.$Modal.confirm({
        title: '确认删除',
        content: '您确认要删除该条帖子吗 ?',
        loading: true,
        onOk: () => {
          removePostById({ postId: v.id }).then(res => {
            this.$Modal.remove();
            if (res.code === 200) {
              this.clearSelectAll();
              this.$Message.success('删除成功');
              this.getDataList();
            }
          });
        }
      });
    },
    showSelect(e) {
      this.selectList = e;
      this.selectList.length = e.length;
    },
    clearSelectAll() {
      this.$refs.table.selectAll(false);
    },
    delAll() {
      if (this.selectList.length <= 0) {
        this.$Message.warning('您还未选择要删除的数据');
        return;
      }
      this.$Modal.confirm({
        title: '确认删除',
        content: '您确认要删除所选的 ' + this.selectList.length + ' 条数据?',
        loading: true,
        onOk: () => {
          let ids = '';
          this.selectList.forEach(function(e) {
            ids += e.id + ',';
          });
          ids = ids.substring(0, ids.length - 1);
          deleteUser({ ids: ids }).then(res => {
            this.$Modal.remove();
            if (res.success) {
              this.$Message.success('删除成功');
              this.clearSelectAll();
              this.getDataList();
            }
          });
        }
      });
    }
  },
  mounted() {
    // 计算高度
    this.height = Number(document.documentElement.clientHeight - 230);
    this.init();
  }
};
</script>
